home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / progs / editor / frexxed / fpl / incsearch.fpl < prev    next >
Text File  |  1995-07-27  |  4KB  |  125 lines

  1. // $Id: IncSearch.FPL 1.3 1995/07/27 10:18:30 jskov Exp $
  2. // $VER: IncSearch.FPL 1.2 (08.03.95) © Jesper Skov
  3.  
  4. // dir - Initial direction of search
  5. //   -1: Backward
  6. //    1: Forward
  7. //
  8. string lastsearch="";                        // holds string from previous search session
  9.  
  10. void export IncSearch2(int dir)
  11. {
  12.   int cont=1;
  13.   int searchLen = 0;
  14.   int prevLine, prevByte;
  15.   int lenHistory[96];
  16.   int byteHistory[96];
  17.   int lineHistory[96];
  18.   int count = 0;
  19.   int searchflags=ReadInfo("search_flags"); // Keep old search flags
  20.  
  21.   string searchString, key;
  22.  
  23.   if (1==dir)                                // Set inital search direction
  24.     SearchSet("=f+");
  25.   else
  26.     SearchSet("=f-");
  27.  
  28.   byteHistory[0]=ReadInfo("byte_position");    // Get initial position
  29.   lineHistory[0]=ReadInfo("line");
  30.   lenHistory[0]=0;
  31.  
  32.   while (cont){
  33.     Status(0, joinstr("Search:»",searchString,"«"));
  34.     key = GetKey();
  35.     if (!strcmp(key,"\x07") || !strcmp(key, "\x1b")){    // ESC or C-g -> Cancel search
  36.       GotoLine(lineHistory[0], byteHistory[0]);
  37.       cont = 0;
  38.  
  39.     } else if (!strcmp(key, "\b")){         // Backspace
  40.       if (searchLen && count){
  41.         count--;
  42.         searchLen=lenHistory[count];
  43.         searchString=substr(searchString, 0, searchLen);
  44.         GotoLine(lineHistory[count], byteHistory[count]);
  45.         Status(0,"",2);
  46.       }
  47.  
  48.     } else if (count<96){                    // Only proceed if enough table space!
  49.  
  50.       if (!strcmp(key, "\x13")){            // C-s -> search forward
  51.         dir = 1;
  52.         if (!searchLen && ReadInfo("search_forward")){
  53.           searchString=lastsearch;            // Get old string if C-r/s C-s
  54.           searchLen=strlen(searchString);
  55.         }
  56.         SearchSet("f+");
  57.         if (searchLen)
  58.           if (Search(searchString)){
  59.             DisplayBeep();
  60.           } else {
  61.             Status(0,"",2);
  62.             count++;                            // Inc one for successfull continued search
  63.             lenHistory[count]=searchLen;
  64.             byteHistory[count]=ReadInfo("byte_position");
  65.             lineHistory[count]=ReadInfo("line");
  66.           }
  67.       } else if (!strcmp(key, "\x12")){     // C-r -> search backwards
  68.         dir = -1;
  69.         if (!searchLen && !ReadInfo("search_forward")) {
  70.           searchString=lastsearch;            // Get old string if C-r/s C-r
  71.           searchLen=strlen(searchString);
  72.         }
  73.         SearchSet("f-");
  74.         if (searchLen) {
  75.           if (Search(searchString)){
  76.             DisplayBeep();
  77.           } else {
  78.             Status(0,"",2);
  79.             count++;                            // Inc one for successfull continued search
  80.             lenHistory[count]=searchLen;
  81.             byteHistory[count]=ReadInfo("byte_position");
  82.             lineHistory[count]=ReadInfo("line");
  83.           }
  84.         }
  85.       } else { // Check if we're dealing with a printable [spc-z]
  86.         if ((0<=strcmp(key, " "))&&(0>=strcmp(key, "z"))&&(searchLen<33)){
  87.           searchLen++;
  88.           searchString+=key;
  89.           prevLine=ReadInfo("line");
  90.           prevByte=ReadInfo("byte_position");
  91.           if (1==dir){
  92.             CursorLeft();                    // Prepare forward search
  93.           } else {
  94.             CursorRight(searchLen);            // Prepare backwards search
  95.           }
  96.           count++;
  97.           lenHistory[count]=searchLen;
  98.           if (Search(searchString)){        // If no luck re-position cursor
  99.             if (1==dir){
  100.               CursorRight();
  101.             } else {
  102.               CursorLeft(searchLen);
  103.             }
  104.             DisplayBeep();
  105.           }
  106.           Status(0,"",2);
  107.           byteHistory[count]=ReadInfo("byte_position"); // Get new position
  108.           lineHistory[count]=ReadInfo("line");
  109.         } else {                            // Non-printable -> quit
  110.           cont=0;
  111.         }
  112.       }
  113.     }
  114.   }
  115.   if (searchLen)
  116.     lastsearch=searchString;                // Remember searchstring
  117.   SetInfo(0, "search_flags", searchflags);    // Restore search flags
  118. }
  119.  
  120. AssignKey("IncSearch2(-1);","control r");
  121. AssignKey("IncSearch2(1);","control s");
  122.  
  123.  
  124. // Ideas:        Wrapped search (C-s after fail -> wrap buffer)
  125.